import { ApiCall } from "tsrpc";
import { ReqGetOneData, ResGetOneData } from "../shared/protocols/PtlGetOneData";
import { Global } from "../models/Global";

// This is a demo code file
// Feel free to delete it

export default async function (call: ApiCall<ReqGetOneData, ResGetOneData>) {
    const searchParam = call.req.ScanNum; // 假设请求的参数字段名为 ScanNum

    // 首先尝试通过 AssetNum 搜索
    // let op = await Global.db.collection("Asset").findOne({ AssetNum: searchParam });

    // if (!op) {
    //     // 如果没有找到，则尝试通过 SN 搜索
    //     op = await Global.db.collection("Asset").findOne({ SerialNum: searchParam });
    // }
    const op = await Global.db.collection("Asset").findOne({
        $or: [
            { AssetNum: searchParam },
            { SerialNum: searchParam }
        ]
    });
    if (op) {
        call.succ({
            data: {
                Flag: true,
                AssetNum: op.AssetNum,
                SerialNum: op.SerialNum,
                EquipmentName: op.EquipmentName,
                EquipmentModel: op.EquipmentModel
            }
        });
    } else {
        call.succ({
            data: {
                Flag: false,
                AssetNum: "扫描的该条码不在MES资产盘点范围内",
                SerialNum: searchParam,
                EquipmentName: ""
            }
        });
    }
}
